<!DOCTYPE html>
<html lang="en">
	<head>
		<title>three.js webgl - lights - physical lights</title>
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
		<style>
			body {
				background-color: #000;
				margin: 0px;
				overflow: hidden;
			}

			#info {
				position: absolute;
				top: 0px; width: 100%;
				color: #ffffff;
				padding: 5px;
				font-family: Monospace;
				font-size: 13px;
				text-align: center;
			}

			a {
				color: #ff0080;
				text-decoration: none;
			}

			a:hover {
				color: #0080ff;
			}
		</style>
	</head>
	<body>

		<div id="container"></div>
		<div id="info">
			<a href="http://threejs.org" target="_blank">three.js</a> - Physically accurate lighting example using a incandescent bulb - by <a href="http://clara.io" target="_blank">Ben Houston</a><br />
			Using real world scale: Brick cube is 1 meter in size.  Light is 2 meters from floor.  Globe is 25 cm in diameter.<br/>
			Using Reinhard inline tonemapping with real-world light falloff (decay = 2).
		</div>

    <script src="js/webvr-polyfill.js"></script>
		<script src="js/three.min.js"></script>
    <script src="js/VRControls.js"></script>
		<!-- <script src="js/stats.min.js"></script> -->
		<script src="js/dat.gui.min.js"></script>
		<script src="js/OrbitControls.js"></script>
		<script src="js/Detector.js"></script>
		<script src="js/OBJLoader.js"></script>
		<!-- <script src="js/ColladaLoader2.js"></script> -->

		<script>

			if ( ! Detector.webgl ) Detector.addGetWebGLMessage();

			var camera, scene, renderer,
			bulbLight, bulbMat, ambientLight,
			object, loader, stats, controls;
			var ballMat, cubeMat, floorMat;
			var rectLight, rectLightHelper;
			var dae;

			// ref for lumens: http://www.power-sure.com/lumens.htm
			var bulbLuminousPowers = {
				"110000 lm (1000W)": 110000,
				"3500 lm (300W)": 3500,
				"1700 lm (100W)": 1700,
				"800 lm (60W)": 800,
				"400 lm (40W)": 400,
				"180 lm (25W)": 180,
				"20 lm (4W)": 20,
				"Off": 0
			};

			// ref for solar irradiances: https://en.wikipedia.org/wiki/Lux
			var hemiLuminousIrradiances = {
				"0.0001 lx (Moonless Night)": 0.0001,
				"0.002 lx (Night Airglow)": 0.002,
				"0.5 lx (Full Moon)": 0.5,
				"3.4 lx (City Twilight)": 3.4,
				"50 lx (Living Room)": 50,
				"100 lx (Very Overcast)": 100,
				"350 lx (Office Room)": 350,
				"400 lx (Sunrise/Sunset)": 400,
				"1000 lx (Overcast)": 1000,
				"18000 lx (Daylight)": 18000,
				"50000 lx (Direct Sun)": 50000
			};

			var params = {
				shadows: true,
				exposure: 0.68,
				bulbPower: Object.keys( bulbLuminousPowers )[ 2 ],
				hemiIrradiance: Object.keys( hemiLuminousIrradiances )[0]
			};


			var clock = new THREE.Clock();
			// var controls = new THREE.VRControls( camera );

			init();
			animate();


			// var loader = new THREE.ColladaLoader();
			// loader.options.convertUpAxis = true;
			// loader.load( './models/json/chair.dae', function ( collada ) {
			// 	dae = collada.scene;
			// 	dae.traverse( function ( child ) {
			// 		if ( child instanceof THREE.SkinnedMesh ) {
			// 			var animation = new THREE.Animation( child, child.geometry.animation );
			// 			animation.play();
			// 		}
			// 	} );
			// 	dae.scale.x = dae.scale.y = dae.scale.z = 0.02;
			// 	dae.updateMatrix();
			// 	init();
			// 	animate();
			// } );


				// texture
				// var manager = new THREE.LoadingManager();
				// manager.onProgress = function ( item, loaded, total ) {
				// 	console.log( item, loaded, total );
				// };

				// var texture = new THREE.Texture();
				// var onProgress = function ( xhr ) {
				// 	if ( xhr.lengthComputable ) {
				// 		var percentComplete = xhr.loaded / xhr.total * 100;
				// 		console.log( Math.round(percentComplete, 2) + '% downloaded' );
				// 	}
				// };

				// var onError = function ( xhr ) {
				// };

				// var loader = new THREE.ImageLoader( manager );
				// loader.load( 'models/obj/Sofa_obj/Leather0071_1_M.jpg', function ( image ) {
				// 	texture.image = image;
				// 	texture.needsUpdate = true;
				// } );

				


			// model
				// var loader = new THREE.OBJLoader( manager );
				// loader.load( 'models/obj/B sofa/B sofa.obj', function ( object ) {
				// 	object.traverse( function ( child ) {
				// 		if ( child instanceof THREE.Mesh ) {
				// 			console.log("CHILD: ", child)
				// 			child.material.map = texture;
				// 			var textureLoader = new THREE.TextureLoader();
				// 			textureLoader.load( "models/obj/B sofa/bsofa/14_5_28_d.jpg", function( map ) {
				// 				map.wrapS = THREE.RepeatWrapping;
				// 				map.wrapT = THREE.RepeatWrapping;
				// 				map.anisotropy = 4;
				// 				map.repeat.set( 10, 24 );
				// 				child.material.map = map;
				// 				child.material.needsUpdate = true;
				// 			} );

				// 			textureLoader.load( "models/obj/B sofa/bsofa/14_5_28_b.jpg", function( map ) {
				// 				map.wrapS = THREE.RepeatWrapping;
				// 				map.wrapT = THREE.RepeatWrapping;
				// 				// map.anisotropy = 4;
				// 				// map.repeat.set( 10, 24 );
				// 				child.material.bumpMap = map;
				// 				child.material.needsUpdate = true;
				// 			} );
				// 		}
				// 	} );
				// 	// object.position.y = - 5;
				// 	object.scale.x = object.scale.y = object.scale.z = 1;
				// 	scene.add( object );
				// }, onProgress, onError );


			function init() {

				var container = document.getElementById( 'container' );

				// stats = new Stats();
				// container.appendChild( stats.dom );


				camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 0.1, 100 );
				camera.position.x = -4;
				camera.position.z = 4;
				camera.position.y = 2;

				scene = new THREE.Scene();

				var ambient = new THREE.AmbientLight( 0x444444, 5 );
				scene.add( ambient );
				// var directionalLight = new THREE.DirectionalLight( 0xffeedd );
				// directionalLight.position.set( 0, 0, 1 ).normalize();
				// scene.add( directionalLight );

				var bulbGeometry = new THREE.SphereGeometry( 0.02, 16, 8 );
				var bulbBoxGeometry = new THREE.BoxGeometry( 8, 0.5, 0.002 );
				bulbLight = new THREE.PointLight( 0xffee88, 1, 100, 2 );

				bulbMat = new THREE.MeshStandardMaterial( {
					emissive: 0xffffee,
					emissiveIntensity: 1,
					color: 0x000000
				});
				bulbLight.add( new THREE.Mesh( bulbBoxGeometry, bulbMat ) );
				bulbLight.position.set( 0, 5, -10 );
				bulbLight.castShadow = true;
				scene.add( bulbLight );

				hemiLight = new THREE.HemisphereLight( 0xddeeff, 0x0f0e0d, 0.02 );
				scene.add( hemiLight );

				floorMat = new THREE.MeshStandardMaterial( {
					roughness: 0.8,
					color: 0xffffff,
					metalness: 0.2,
					bumpScale: 0.0005
				});

				// var diffuseColor = new THREE.Color().setHSL( alpha, 0.5, 0.5 );
				var diffuseColor = new THREE.Color().setHex( 0x3498db );

				wallMat1 = new THREE.MeshPhysicalMaterial( {
					// roughness: 0.8,
					color: diffuseColor,
					// metalness: 0.2,
					// bumpScale: 0.0005
				});

				wallMat2 = new THREE.MeshPhongMaterial( {
					// roughness: 0.8,
					color: new THREE.Color().setHex( 0xf1c40f ),
					// metalness: 0.2,
					// bumpScale: 0.0005
				});

				wallMat3 = new THREE.MeshPhongMaterial( {
					// roughness: 0.8,
					color: new THREE.Color().setHex( 0xe67e22 ),
					// metalness: 0.2,
					// bumpScale: 0.0005
				});

				wallMat4 = new THREE.MeshPhongMaterial( {
					// roughness: 0.8,
					color: new THREE.Color().setHex( 0x3498db ),
					// metalness: 0.2,
					// bumpScale: 0.0005
				});

				ceilingMat = new THREE.MeshPhongMaterial( {
					roughness: 0.8,
					color: new THREE.Color().setHex( 0xecf0f1 ),
					// metalness: 0.2,
					// bumpScale: 0.0005
				});

				var textureLoader = new THREE.TextureLoader();
				textureLoader.load( "textures/hardwood2_diffuse.jpg", function( map ) {
					map.wrapS = THREE.RepeatWrapping;
					map.wrapT = THREE.RepeatWrapping;
					map.anisotropy = 4;
					map.repeat.set( 10, 24 );
					floorMat.map = map;
					floorMat.needsUpdate = true;
				} );
				textureLoader.load( "textures/hardwood2_bump.jpg", function( map ) {
					map.wrapS = THREE.RepeatWrapping;
					map.wrapT = THREE.RepeatWrapping;
					map.anisotropy = 4;
					map.repeat.set( 10, 24 );
					floorMat.bumpMap = map;
					floorMat.needsUpdate = true;
				} );
				textureLoader.load( "textures/hardwood2_roughness.jpg", function( map ) {
					map.wrapS = THREE.RepeatWrapping;
					map.wrapT = THREE.RepeatWrapping;
					map.anisotropy = 4;
					map.repeat.set( 10, 24 );
					floorMat.roughnessMap = map;
					floorMat.needsUpdate = true;
				} );

				// cubeMat = new THREE.MeshStandardMaterial( {
				// 	roughness: 0.7,
				// 	color: 0xffffff,
				// 	bumpScale: 0.002,
				// 	metalness: 0.2
				// });
				// textureLoader.load( "textures/brick_diffuse.jpg", function( map ) {
				// 	map.wrapS = THREE.RepeatWrapping;
				// 	map.wrapT = THREE.RepeatWrapping;
				// 	map.anisotropy = 4;
				// 	map.repeat.set( 1, 1 );
				// 	cubeMat.map = map;
				// 	cubeMat.needsUpdate = true;
				// } );
				// textureLoader.load( "textures/brick_bump.jpg", function( map ) {
				// 	map.wrapS = THREE.RepeatWrapping;
				// 	map.wrapT = THREE.RepeatWrapping;
				// 	map.anisotropy = 4;
				// 	map.repeat.set( 1, 1 );
				// 	cubeMat.bumpMap = map;
				// 	cubeMat.needsUpdate = true;
				// } );

				// ballMat = new THREE.MeshStandardMaterial( {
				// 	color: 0xffffff,
				// 	roughness: 0.5,
				// 	metalness: 1.0
				// });
				// textureLoader.load( "textures/earth_atmos_2048.jpg", function( map ) {
				// 	map.anisotropy = 4;
				// 	ballMat.map = map;
				// 	ballMat.needsUpdate = true;
				// } );
				// textureLoader.load( "textures/earth_specular_2048.jpg", function( map ) {
				// 	map.anisotropy = 4;
				// 	ballMat.metalnessMap = map;
				// 	ballMat.needsUpdate = true;
				// } );

				var floorGeometry = new THREE.PlaneBufferGeometry( 20, 20 );
				var floorMesh = new THREE.Mesh( floorGeometry, floorMat );
				floorMesh.receiveShadow = true;
				floorMesh.position.set( 0, -5, 0);
				floorMesh.rotation.x = -Math.PI / 2.0;
				scene.add( floorMesh );

				var ceilingGeometry = new THREE.PlaneBufferGeometry( 20, 20 );
				var ceilingMesh = new THREE.Mesh( ceilingGeometry, ceilingMat );
				ceilingMesh.receiveShadow = true;
				ceilingMesh.position.set( 0, 5, 0);
				ceilingMesh.rotation.x = Math.PI / 2.0;
				scene.add( ceilingMesh );


				var wallGeometry = new THREE.PlaneBufferGeometry( 20, 10);
				var wallMesh1 = new THREE.Mesh( wallGeometry, wallMat1 );
				wallMesh1.receiveShadow = true;
				wallMesh1.position.set( 0, 0, -10 );
				// wallMesh1.rotation.x = -Math.PI / 2.0;
				// wallMesh1.rotation.y = -Math.PI / 2.0;
				// wallMesh1.rotation.z = -Math.PI / 2.0;
				// scene.add( wallMesh1 );

				var wallGeometry = new THREE.PlaneBufferGeometry( 20, 10 );
				var wallMesh2 = new THREE.Mesh( wallGeometry, wallMat2 );
				wallMesh2.receiveShadow = true;
				wallMesh2.position.set( 10, 0, 0 );
				// wallMesh2.rotation.x = -Math.PI / 2.0;
				wallMesh2.rotation.y = -Math.PI / 2.0;
				// wallMesh2.rotation.z = -Math.PI / 2.0;
				// scene.add( wallMesh2 );

				var wallGeometry = new THREE.PlaneBufferGeometry( 20, 10 );
				var wallMesh3 = new THREE.Mesh( wallGeometry, wallMat3 );
				wallMesh3.receiveShadow = true;
				wallMesh3.position.set( 0, 0, 10 );
				// wallMesh3.rotation.x = -Math.PI / 2.0;
				wallMesh3.rotation.y = -Math.PI;
				// wallMesh3.rotation.z = -Math.PI / 2.0;
				// scene.add( wallMesh3 );
				
				var wallGeometry = new THREE.PlaneBufferGeometry( 20, 10 );
				var wallMesh4 = new THREE.Mesh( wallGeometry, wallMat4 );
				wallMesh4.receiveShadow = true;
				wallMesh4.position.set( -10, 0, 0 );
				// wallMesh4.rotation.x = -Math.PI / 2.0;
				wallMesh4.rotation.y = Math.PI / 2.0;
				// wallMesh4.rotation.z = -Math.PI / 2.0;
				// scene.add( wallMesh4 );



				// var ballGeometry = new THREE.SphereGeometry( 0.5, 32, 32 );
				// var ballMesh = new THREE.Mesh( ballGeometry, ballMat );
				// ballMesh.position.set( 1, -5, 1 );
				// ballMesh.rotation.y = Math.PI;
				// ballMesh.castShadow = true;
				// scene.add( ballMesh );

				// var boxGeometry = new THREE.BoxGeometry( 0.5, 0.5, 0.5 );
				// var boxMesh = new THREE.Mesh( boxGeometry, cubeMat );
				// boxMesh.position.set( -0.5, -5, -1 );
				// boxMesh.castShadow = true;
				// scene.add( boxMesh );

				// var boxMesh2 = new THREE.Mesh( boxGeometry, cubeMat );
				// boxMesh2.position.set( 0, 0.25, -5 );
				// boxMesh2.castShadow = true;
				// scene.add( boxMesh2 );

				// var boxMesh3 = new THREE.Mesh( boxGeometry, cubeMat );
				// boxMesh3.position.set( 7, 0.25, 0 );
				// boxMesh3.castShadow = true;
				// scene.add( boxMesh3 );

				// BEGIN Clara.io JSON loader code
				// var objectLoader = new THREE.ObjectLoader();
				// objectLoader.load("models/json/chair.json", function ( chair ) {
				// 	chair.scale.x = chair.scale.y = chair.scale.z = 0.05;
				// 	chair.position.set(9, -5, 0);
				// 	chair.rotation.y = 70 * Math.PI / 180;
				// 	// chair.rotation.y = Math.PI / 2;
				//  	scene.add( chair );
				// } );

				var jsonLoader = new THREE.JSONLoader();
				jsonLoader.load("models/json/untitled_wood.json", function ( geo, mat ) {
					console.log(geo)
					console.log(mat)
					var sofaMesh = new THREE.Mesh( geo, mat[0] );
					sofaMesh.scale.x = sofaMesh.scale.y = sofaMesh.scale.z = 2;
					scene.add(sofaMesh)

					var textureLoader = new THREE.TextureLoader();
					textureLoader.load( "textures/wood_07_diffuse.jpg", function( map ) {
						map.wrapS = THREE.RepeatWrapping;
						map.wrapT = THREE.RepeatWrapping;
						// map.anisotropy = 4;
						// map.repeat.set( 10, 24 );
						mat[0].map = map;
						mat[0].needsUpdate = true;
						// sofaMesh.position.set(0, -5, -5);
						// sofaMesh.rotation.y = 70 * Math.PI / 180;
						// sofaMesh.rotation.y = Math.PI;
						// scene.add(sofaMesh)
					 	// scene.add( sofa );
					} );

					// textureLoader.load( "textures/14_5_28_b.jpg", function( map ) {
					// 	map.wrapS = THREE.RepeatWrapping;
					// 	map.wrapT = THREE.RepeatWrapping;
					// 	// map.anisotropy = 4;
					// 	// map.repeat.set( 10, 24 );
					// 	mat[0].bumpMap = map;
					// 	mat[0].needsUpdate = true;
					// 	// sofaMesh.position.set(0, -5, -5);
					// 	// sofaMesh.rotation.y = 70 * Math.PI / 180;
					// 	// sofaMesh.rotation.y = Math.PI;
					//  	// scene.add( sofa );
					// } );
					
				} );

				var objectLoader = new THREE.ObjectLoader();
				objectLoader.load("models/json/atelier-pfister-riom-sofa.json", function ( sofa ) {
					sofa.scale.x = sofa.scale.y = sofa.scale.z = 0.3;
					sofa.position.set(0, -4.5, -5);
					// sofa.rotation.y = 70 * Math.PI / 180;
					sofa.rotation.y = Math.PI;
				 	scene.add( sofa );
				} );

				// END Clara.io JSON loader code


				// rectLight = new THREE.RectAreaLight( 0xFFFFFF, undefined, 2, 10 );
				// rectLight.matrixAutoUpdate = true;
				// rectLight.intensity = 800.0;
				// rectLight.position.set( 0, 5, -9.2 );
				// rectLight.rotation.x = Math.PI / 4;
				// rectLight.rotation.z = Math.PI / 2;
				// // TODO: ensure RectAreaLight handles target param correctly
				// // rectLight.target = mshPhongBox;
				// rectLightHelper = new THREE.RectAreaLightHelper( rectLight );
				// rectLight.add( rectLightHelper );
				// scene.add( rectLight )

				//add the colada
				// scene.add( dae );

				renderer = new THREE.WebGLRenderer();
				renderer.physicallyCorrectLights = true;
				renderer.gammaInput = true;
				renderer.gammaOutput = true;
				renderer.shadowMap.enabled = true;
				renderer.toneMapping = THREE.ReinhardToneMapping;
				renderer.setPixelRatio( window.devicePixelRatio );
				renderer.setSize( window.innerWidth, window.innerHeight );
				container.appendChild( renderer.domElement );


				var controls = new THREE.OrbitControls( camera, renderer.domElement );
				controls.target.set( 0, 0, 0 );
				controls.update();

				// controls = new THREE.VRControls( camera );

				window.addEventListener( 'resize', onWindowResize, false );


				var gui = new dat.GUI();

				gui.add( params, 'hemiIrradiance', Object.keys( hemiLuminousIrradiances ) );
				gui.add( params, 'bulbPower', Object.keys( bulbLuminousPowers ) );
				gui.add( params, 'exposure', 0, 1 );
				gui.add( params, 'shadows' );
				gui.open();
			}

			function onWindowResize() {

				camera.aspect = window.innerWidth / window.innerHeight;
				camera.updateProjectionMatrix();

				renderer.setSize( window.innerWidth, window.innerHeight );

			}

			//

			function animate() {

				requestAnimationFrame( animate );

				render();

				// controls.update();

			}

			var previousShadowMap = false;

			function render() {

				renderer.toneMappingExposure = Math.pow( params.exposure, 5.0 ); // to allow for very bright scenes.
				renderer.shadowMap.enabled = params.shadows;
				bulbLight.castShadow = params.shadows;
				// rectLight.castShadow = params.shadows;
				if( params.shadows !== previousShadowMap ) {
					// ballMat.needsUpdate = true;
					// cubeMat.needsUpdate = true;
					floorMat.needsUpdate = true;
					previousShadowMap = params.shadows;
				}
				bulbLight.power = bulbLuminousPowers[ params.bulbPower ];
				bulbMat.emissiveIntensity = bulbLight.intensity / Math.pow( 0.02, 2.0 ); // convert from intensity to irradiance at bulb surface

				hemiLight.intensity = hemiLuminousIrradiances[ params.hemiIrradiance ];
				var time = Date.now() * 0.0005;
				var delta = clock.getDelta();

				// bulbLight.position.y = Math.cos( time ) * 0.75 + 1.25;
				bulbLight.position.y = 3;

				renderer.render( scene, camera );

				// stats.update();

			}

		</script>
	</body>
</html>